package com.hxit.huaxiamelongarden.service.impl;

import com.hxit.huaxiamelongarden.domin.po.ChatMessage;
import com.hxit.huaxiamelongarden.mapper.ChatMessageMapper;
import com.hxit.huaxiamelongarden.service.ChatMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName ChatMessageServiceImpl
 * @description 聊天实现类
 * @Author 12694
 * @Date 2025/1/30 20:53
 * @Version 1.0
 **/
@Service
public class ChatMessageServiceImpl implements ChatMessageService {
    @Autowired
    private ChatMessageMapper chatMessageMapper;

    private static final Logger logger = LoggerFactory.getLogger(ChatMessageServiceImpl.class);

    // 存储聊天消息
    @Transactional
    public void saveMessage(Integer senderId, Integer receiverId, String message) {
        try {
            ChatMessage chatMessage = new ChatMessage();
            chatMessage.setSenderId(senderId);
            chatMessage.setReceiverId(receiverId);
            chatMessage.setMessage(message);

            // 存储消息到数据库
            chatMessageMapper.insert(chatMessage);

            // 打印日志
            logger.info("成功保存消息，发送者ID：{}，接收者ID：{}", senderId, receiverId); // 打印中文日志
        } catch (Exception e) {
            logger.error("保存消息失败，发送者ID：{}，接收者ID：{}", senderId, receiverId, e); // 打印中文日志和异常信息
            throw new RuntimeException("保存消息失败，数据库错误");
        }
    }

}

